Fix memory corruption vulnerabilities in instrumentation subsystem #279
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes three memory corruption vulnerabilities in the instrumentation subsystem:
1. Missing null-termination after strncpy operations
save_context()copies thread names usingk_thread_name_copy()(which internally usesstrncpy) and directstrncpy()calls without ensuring null termination. When thread names equal or exceedCONFIG_THREAD_MAX_NAME_LEN, the buffer lacks a null terminator, causing undefined behavior on subsequent reads.Fix: Explicit null termination after both copy operations.
2. Integer underflow in call depth tracking
pop_callee_timestamp()unconditionally decrements theuint16_t call_depthfield. Spurious or unbalanced function exits cause underflow (0 → 65535), corrupting profiling state and breaking depth tracking logic.Fix: Guard decrement with zero check, increment
unbalancedcounter for diagnostics.3. Ring buffer size mismatch
Ring buffer initialized with
sizeof(instr_buffer)=CONFIG_SIZE + 1instead of configured size. Users requesting N bytes receive N+1, wasting memory and violating configuration contract.Fix: Initialize with
CONFIG_INSTRUMENTATION_MODE_CALLGRAPH_TRACE_BUFFER_SIZEdirectly.Testing
No existing tests cover this subsystem. Validation performed through code review and static analysis.
Documentation
No documentation changes required.
Installation
No installer impact.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.